package org.railage.server.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.railage.server.HibernateUtil;
import org.railage.server.entities.TripThroughTwoStations;

public class TripThroughTwoStationsDAOImpl implements TripThroughTwoStationsDAO {

	private static final Logger log = Logger
			.getLogger(TripThroughTwoStationsDAOImpl.class);

	@SuppressWarnings("unchecked")
	public List<TripThroughTwoStations> getTripsThroughTwoStations(
			String stationA, String stationB) {
		Session session = null;
		List<TripThroughTwoStations> trips = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Query query = session.getNamedQuery("getTrips_A_B");
			query.setString("station_A", stationA);
			query.setString("station_B", stationB);
			trips = (List<TripThroughTwoStations>) query.list();
		} catch (Exception e) {
			log.error("Error occured during Hibernate session:", e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return trips;
	}

	@SuppressWarnings("unchecked")
	public List<TripThroughTwoStations> getTripsThroughTwoStations(
			String stationA, String stationB, String duration) {
		Session session = null;
		List<TripThroughTwoStations> trips = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Query query = session.getNamedQuery("getTrips_A_B_duration");
			query.setString("station_A", stationA);
			query.setString("station_B", stationB);
			query.setString("duration", duration);
			trips = (List<TripThroughTwoStations>) query.list();
		} catch (Exception e) {
			log.error("Error occured during Hibernate session:", e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return trips;
	}

	@SuppressWarnings("unchecked")
	public List<TripThroughTwoStations> getTripsThroughTwoStations(
			String stationA, String stationB, String departure, String arrival) {
		Session session = null;
		List<TripThroughTwoStations> trips = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Query query = session.getNamedQuery("getTrips_A_B_dep_arr");
			query.setString("station_A", stationA);
			query.setString("station_B", stationB);
			query.setString("departure", departure);
			query.setString("arrival", arrival);
			trips = (List<TripThroughTwoStations>) query.list();
		} catch (Exception e) {
			log.error("Error occured during Hibernate session:", e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return trips;
	}

}
